package com.bawei.module_mine.ui

import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.graphics.Color
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import android.view.animation.AccelerateInterpolator
import androidx.core.animation.doOnEnd
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_mvi.Router
import com.bawei.lib_mvi.base.BaseActivity
import com.bawei.lib_storage.mmkv.AuthMMKV
import com.bawei.module_mine.databinding.ActivityNativeLoginBinding
import com.bawei.module_mine.model.GetPermissions

@Route(path = Router.Ui.NATIVE_LOGIN_ACTIVITY)
class NativeLoginActivity : BaseActivity() {

    private val binding by lazy {
        ActivityNativeLoginBinding.inflate(layoutInflater)
    }

    @SuppressLint("ObjectAnimatorBinding")
    override fun initView() {
        super.initView()
        binding.ivMore.setOnClickListener {
            if (binding.hiddenIconsLayout.visibility == View.GONE){
                binding.hiddenIconsLayout.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED)
                val with = binding.hiddenIconsLayout.measuredWidth
                ObjectAnimator.ofInt(binding.hiddenIconsLayout,"width",0,with)
                    .apply {
                        duration = 300
                        interpolator = AccelerateDecelerateInterpolator()
                        start()
                    }
                binding.hiddenIconsLayout.visibility = View.VISIBLE
                binding.ivMore.visibility = View.GONE

            }else{
                ObjectAnimator.ofInt(binding.hiddenIconsLayout,"with",0)
                    .apply {
                        duration = 300
                        interpolator = AccelerateInterpolator()
                        start()
                        doOnEnd {
                            binding.hiddenIconsLayout.visibility = View.GONE
                            binding.ivClose.visibility = View.VISIBLE
                        }
                    }
            }
        }
        binding.tvHelp.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.HELP_ACTIVITY).navigation()
        }
        val spannable = SpannableStringBuilder(binding.tvPrivacy.text.toString())
        val spannable1 = SpannableStringBuilder(binding.tvProtocol.text.toString())
        val spannable3 = SpannableStringBuilder(binding.tvTk.text.toString())
        spannable.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(Router.Ui.WEB_ACTIVITY)
                    .withString("url","https://terms.alicdn.com/legal-agreement/terms/suit_bu1_unification/suit_bu1_unification202005142208_14749.html")
                    .navigation()
            }

        },0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0, 4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvPrivacy.setText(spannable)
        binding.tvPrivacy.setMovementMethod(LinkMovementMethod.getInstance())
        spannable1.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(Router.Ui.WEB_ACTIVITY)
                    .withString("url","https://www.douyin.com/draft/douyin_agreement/douyin_agreement_privacy.html?id=6773901168964798477")
                    .navigation()
            }

        },0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable1.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0,4, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvProtocol.setText(spannable1)
        binding.tvProtocol.setMovementMethod(LinkMovementMethod.getInstance())
        spannable3.setSpan(object : ClickableSpan(){
            override fun onClick(p0: View) {
                ARouter.getInstance().build(Router.Ui.WEB_ACTIVITY)
                    .withString("url","https://wap.cmpassport.com/resources/html/contract1.html")
                    .navigation()
            }

        },0,12, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        spannable3.setSpan(ForegroundColorSpan(Color.parseColor("#807F7F")), 0,12, Spanned.SPAN_EXCLUSIVE_INCLUSIVE)
        binding.tvTk.setText(spannable3)
        binding.tvTk.setMovementMethod(LinkMovementMethod.getInstance())
        binding.ivClose.setOnClickListener{
            finish()
        }
        binding.btnLogin.setOnClickListener {
            if (GetPermissions.hasAllPermissions(this)){
                ARouter.getInstance().build(Router.Ui.MainActivity).navigation()
            }else{
                ARouter.getInstance().build(Router.Ui.PHONE_BOOK_ACTIVITY).navigation()
            }
        }
        binding.btnOtherLogin.setOnClickListener {
            ARouter.getInstance().build(Router.Ui.PHONE_LOGIN_ACTIVITY).navigation()
        }
    }

    override fun onResume() {
        super.onResume()
        val phone = AuthMMKV.username
        if (!phone.isNullOrEmpty()){
            binding.userPhone.setText(GetPermissions.desensitizePhoneNumber(phone))
        }
    }

    override fun getLayoutRootView(): View {
        return binding.root
    }

}